<?php
/**
 * ScoreModel.class.php
 * 积分相关操作模型
 *
 * @author  王中艺
 * @date    2016-11-09
 */

namespace Wechat\Model;

class ScoreModel extends BaseModel {

    //重定义数据表明
    protected   $trueTableName  = 't_score';

    function payScore($userId, $goodsType, $goodsId, $score, $remark = null){
        $userInfo   = D('User')->getUserInfo($userId);
        if($userInfo['score'] < $score){
            $this->errMsg   = '用户积分不足';
            return false;
        }

        $this->startTrans();
        $payInfo    = array(
            'user_id'   => $userId,
            'goods_type'    => $goodsType,
            'goods_id'  => $goodsId,
            'score' => -abs($score),
            'create_time'   => time(),
            'status'    => 1,
            'remark'    => $remark
        );

        $payId  = $this->add($payInfo);
        if(!$payId){
            $this->rollback();
            $this->errMsg   = '数据写入失败(t_score)!';
            return false;
        }

        //用户积分更改
        $userInfo['score']  = $userInfo['score'] - abs($score);
        if(!D('User')->updateUserInfo($userId, $userInfo)){
            $this->rollback();
            $this->errMsg   = '用户积分更改失败!';
            return false;
        }

        $this->commit();
        
        //推送模版消息
        $wxTempMsg  = new \Wechat\Util\WxTempMsg();
        $wxTempMsg->sendScoreTempMsg($userInfo, $score, $goodsType);
        
        $payInfo['id']  = $payId;
        return $payInfo;
    }

    /**
     * 积分获取
     * @param   $userId
     * @param   $goodsType
     * @param   $goodsId
     * @param   $score
     * @return  boolean
     */
    function addScore($userId, $goodsType, $goodsId, $score, $remark = null, $stopTrans = false){
        //检测及分是否重复获取
        $isExt  = $this
            ->where('user_id = %d and goods_type = %d and goods_id = %d', $userId, $goodsType, $goodsId)
            ->find();
        if($isExt)  return true;
        
        $info   = array(
            'user_id'   => $userId,
            'goods_type'    => $goodsType,
            'goods_id'  => $goodsId,
            'score' => abs($score),
            'create_time'   => time(),
            'remark'    => $remark
        );

        $stopTrans or $this->startTrans();

        if(!$this->add($info)){
            $stopTrans or $this->rollback();
            $this->errMsg   = '积分信息插入失败(t_score)!';
            return false;
        }

        //用户积分添加
        $dUser  = D('User');
        $userInfo   = $dUser->getUserInfo($userId);
        $userInfo['score']  += $score;
        if(!$dUser->updateUserInfo($userId, $userInfo)){
            $stopTrans or $this->rollback();
            $this->errMsg   = $dUser->errMsg;
            return false;
        }

        $stopTrans or $this->commit();

        //推送模版消息
        $wxTempMsg  = new \Wechat\Util\WxTempMsg();
        $wxTempMsg->sendScoreTempMsg($userInfo, $score, $goodsType);

        return true;
    }

    /**
     * 积分收支记录
     * @param   $userId
     * @param   $page
     * @return  $list
     */
    function getLog($userId, $page){
        $list   = $this->where('user_id = %d', $userId)
            ->page($page, C('page_count'))
            ->order('id desc')
            ->select();

        foreach($list as &$v){
            $v['goods_type_text']   = C('score_goods_type.'.$v['goods_type']);
            $v['create_date']   = date('Y-m-d H:i:s', $v['create_time']);
            $v['type'] = $v['score'] > 0 ? '收入' : '支出'; 
        }

        return $list;
    }
}